package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.AdLiveRoomParamDTO;
import com.yuanfeng.base.dto.AddLiveGoodsParamDTO;
import com.yuanfeng.base.dto.WxEstablishLiveRoomDTO;
import com.yuanfeng.base.entity.BaseWxLiveStudioEntity;
import com.yuanfeng.base.mapper.BaseWxLiveStudioMapper;
import com.yuanfeng.base.service.BaseWxLiveStudioService;
import com.yuanfeng.base.utils.AccessTokenUtil;
import com.yuanfeng.base.vo.AdLiveRoomVO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.wx.HttpUtlis;
import com.yuanfeng.commoms.util.wx.ImageUtils;
import com.yuanfeng.commoms.util.wx.NewWxMaterialResult;
import com.yuanfeng.commoms.util.wx.UploadWxUtil;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseWxLiveStudioServiceImpl
 * @Description
 * @Author liulh
 * @Date 2023/7/28 10:56
 * @Version 1.0
 **/
@Service
public class BaseWxLiveStudioServiceImpl extends ServiceImpl<BaseWxLiveStudioMapper, BaseWxLiveStudioEntity>
        implements BaseWxLiveStudioService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${fontUrlPath}")
    private String urlPath;

    @Override
    public ResponseResult addLiveRoom(AdLiveRoomParamDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null == userInfo) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        Integer integer = this.baseMapper.addLiveRoom(param);
        if (integer > 0) {
            //创建直播键
            Map<String,Object> map = new HashMap<>();
            map.put("id",param.getId());
            map.put("shopId",param.getShopId());
            ResponseResult responseResult = wxFoundLiveRoom(map);
            return ResponseResult.success(responseResult);
        }
        return ResponseResult.fail(2,"操作失败");
    }

    public ResponseResult wxFoundLiveRoom(Map<String,Object> map){
        try {
            HttpUtlis httpUtlis = new HttpUtlis();
            HttpUtlis httpUtlis1 = new HttpUtlis();
            HttpUtlis httpUtlis2 = new HttpUtlis();
            String host = "https://api.weixin.qq.com";
            String accessToken = AccessTokenUtil.queryAccessToken();
            String addpath = "/wxaapi/broadcast/room/create?access_token=" + accessToken;
            WxEstablishLiveRoomDTO wxEstablishLiveRoom = this.baseMapper.queryLiveRoom(Integer.valueOf(map.get("id").toString()));
            Map<String, String> stringMap = httpUtlis.doGetImages(wxEstablishLiveRoom.getCoverImg(), 2,urlPath);
            String picName = stringMap.get("pathName");
            Integer integer = null;

            integer = ImageUtils.judgeIsThumbnail(picName);

            if (integer == 2) {
                return ResponseResult.fail(2, "该图片不支持缩略图");
            }
            NewWxMaterialResult materialResult = UploadWxUtil.uploadFile(picName, accessToken);
            wxEstablishLiveRoom.setCoverImg(materialResult.getMediaId());
            Map<String, String> stringMap1 = httpUtlis1.doGetImages(wxEstablishLiveRoom.getShareImg(), 3,urlPath);
            String picName1 = stringMap1.get("pathName");
            Integer integer1 = ImageUtils.judgeIsThumbnail(picName1);
            if (integer1 == 2) {
                return ResponseResult.fail(2, "该图片不支持缩略图");
            }
            NewWxMaterialResult materialResult1 = UploadWxUtil.uploadFile(picName1, accessToken);
            wxEstablishLiveRoom.setShareImg(materialResult1.getMediaId());

            Map<String, String> stringMap2 = httpUtlis2.doGetImages(wxEstablishLiveRoom.getFeedsImg(), 4,urlPath);
            String picName2 = stringMap2.get("pathName");
            Integer integer2 = ImageUtils.judgeIsThumbnail(picName2);
            if (integer2 == 2) {
                return ResponseResult.fail(2, "该图片不支持缩略图");
            }
            NewWxMaterialResult materialResult2 = UploadWxUtil.uploadFile(picName2, accessToken);
            wxEstablishLiveRoom.setFeedsImg(materialResult2.getMediaId());

            AdLiveRoomVO result = restTemplate.postForObject(host + addpath, wxEstablishLiveRoom, AdLiveRoomVO.class);
            if (result.getErrcode() == 0) {
                this.addLiveGoods(Integer.valueOf(map.get("shopId").toString()), result.getRoomId());
                return ResponseResult.success("创建直播间成功", result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.fail(2,"创建直播间失败");
    }

    public Boolean addLiveGoods(Integer shopId,Integer roomId){
        Boolean b = false;
        AddLiveGoodsParamDTO addLiveGoodsParam = new AddLiveGoodsParamDTO();
        List<Integer> integers = this.baseMapper.queryLiveRoomId(shopId);
        if (integers.size() != 0) {
            addLiveGoodsParam.setIds(integers);
            addLiveGoodsParam.setRoomId(roomId);
            String accessToken = AccessTokenUtil.queryAccessToken();
            String url = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token=";
            String result = restTemplate.postForObject(url + accessToken, addLiveGoodsParam, String.class);
            System.out.println(result);
            if (null != result) {
                b = true;
            }
        }
        return b;
    }
}
